//
//  Sector.h
//  trail-map-editor
//
//  Created by Tino Giorgio on 12/3/13.
//
//

#ifndef __trail_map_editor__Sector__
#define __trail_map_editor__Sector__

#include <plib/ul.h>
#include <plib/sg.h>
#include <plib/ssg.h>
#include <plib/ssgAux.h>

class Sector : public ssgTransform
{
public:
    virtual ~Sector();

	//
	// Factory methods, to load and save.
	//
    static Sector* loadFromDEM(const char* filename, bool centerOnOrigin=TRUE);
	static Sector* loadFromBinary(const char* filename, bool centerOnOrigin=TRUE);
	static bool convertDEMToBinary(const char* input, const char* output);
     
    SGfloat getWidth() const { return (mColumns - 1) * mGridStep; }
    SGfloat getHeight() const { return (mRows - 1) * mGridStep; }
	
	void centerOnOrigin() {     
		sgVec3 centeredTransform;
		sgSetVec3(centeredTransform, - (this->getWidth() / 2.0), - (this->getHeight() / 2.0), 0);
		this->setTransform(centeredTransform); 
	}
    
    // Map adjustment
    static sgFloat smMapOffsetU;
    static sgFloat smMapOffsetV;
    
    // Wireframe
    static bool smWireframeEnabled;
    
    // Debug and utils
    void print() const;
    
private:
	Sector(const char* name, int east, int north, int r, int c, int gridStep, int gridThinFactor);
	
    char* mName;
    int mEast; // DEM coord x
    int mNorth; // DEM coord y
    int mColumns;
    int mRows;
    int mGridStep;
    int mGridThinFactor;
    
    ssgSimpleState* mState;
    
	//
	// Used by factory methods
	//
	static sgVec3** _makeNormalMap(sgFloat** heightMap, int r, int c, float gridStep);
    static ssgVtxTable* _createTerrain(sgFloat** heightMap, sgVec3** normalMap, int r, int c, float gridStep);
    static ssgVtxTable* _createBox(sgFloat** heightMap, int r, int c, float gridStep);
    static ssgVtxTable* _createBottom(sgFloat** heightMap, int r, int c, float gridStep);
    static bool _isBorder(sgFloat** heightMap, bool** visited, int r, int c, int x, int y);
    
	//
	// Wireframe 
	//
    static int _callbackPostDraw( ssgEntity* e );
	static int _callbackDrawNormals(ssgEntity* e);
};

#endif /* defined(__trail_map_editor__Sector__) */
